package com.ruoyi.business.controller;

import com.ruoyi.business.domain.Binding;
import com.ruoyi.business.domain.enums.GenderEnum;
import com.ruoyi.business.domain.vo.FrontStudentBindingListVO;
import com.ruoyi.business.service.IBindingService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.StudentBinding;
import com.ruoyi.business.service.IStudentBindingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生绑定Controller
 * 
 * @author lcz
 * @date 2025-02-27
 */
@Api(tags = "学生绑定相关接口")
@RestController
@RequestMapping("/business/studentBinding")
public class StudentBindingController extends BaseController
{
    @Autowired
    private IStudentBindingService studentBindingService;

    @Autowired
    private IBindingService bindingService;

    /**
     * 查询学生绑定列表
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:studentBinding:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentBinding studentBinding)
    {
        startPage();
        List<StudentBinding> list = studentBindingService.selectStudentBindingList(studentBinding);
        return getDataTable(list);
    }

    /**
     * 导出学生绑定列表
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:studentBinding:export')")
    @Log(title = "学生绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentBinding studentBinding)
    {
        List<StudentBinding> list = studentBindingService.selectStudentBindingList(studentBinding);
        ExcelUtil<StudentBinding> util = new ExcelUtil<StudentBinding>(StudentBinding.class);
        util.exportExcel(response, list, "学生绑定数据");
    }

    /**
     * 获取学生绑定详细信息
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:studentBinding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentBindingService.selectStudentBindingById(id));
    }

    /**
     * 新增学生绑定
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:studentBinding:add')")
    @Log(title = "学生绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentBinding studentBinding)
    {
        return toAjax(studentBindingService.insertStudentBinding(studentBinding));
    }

    /**
     * 修改学生绑定
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:studentBinding:edit')")
    @Log(title = "学生绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentBinding studentBinding)
    {
        return toAjax(studentBindingService.updateStudentBinding(studentBinding));
    }

    /**
     * 删除学生绑定
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:studentBinding:remove')")
    @Log(title = "学生绑定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentBindingService.deleteStudentBindingByIds(ids));
    }

    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/

    /**
     * 查询学生绑定列表
     * 接口路径：/studentBinding/front/list
     */
    @GetMapping("/front/studentBindinglist")
    @ApiOperation("前台查询学生绑定列表")
    public TableDataInfo<FrontStudentBindingListVO> frontStudentBindingList(@RequestParam("testSessionId") Long testSessionId)
    {
        startPage();
        List<FrontStudentBindingListVO> list = studentBindingService.frontStudentBindingList(testSessionId);
        list.stream().forEach(item -> {
            item.setGender(GenderEnum.fromKey(Integer.valueOf(item.getGender())).getLabel());
        });
        return getDataTable(list);
    }
//
//    /**
//     * 获取学生绑定详细信息
//     * 接口路径：/studentBinding/front/info
//     */
//    @ApiOperation("前台获取学生绑定详细信息")
//    @GetMapping(value = "/front/info")
//    public R<StudentBinding> frontInfo(@RequestParam("id") Long id)
//    {
//        return R.ok(studentBindingService.selectStudentBindingById(id));
//    }
//
    /**
     * 新增学生绑定
     * 接口路径：/studentBinding/front/add
     */
    @ApiOperation("前台新增学生绑定")
    @PostMapping("/front/add")
    public AjaxResult frontAdd(@RequestBody StudentBinding studentBinding)
    {
        Long schoolId = SecurityUtils.getLoginUser().getUser().getSchoolId();
        StudentBinding binding = new StudentBinding();
        binding.setStudentId(studentBinding.getStudentId());
        binding.setCode(studentBinding.getCode());
        binding.setSchoolId(schoolId);
        List<StudentBinding> list = studentBindingService.selectStudentBindingList(binding);
        if (list.size() > 0) {
            return AjaxResult.error("该学生已绑定，请勿重复绑定");
        }
        studentBinding.setSchoolId(schoolId);
        return toAjax(studentBindingService.insertStudentBinding(studentBinding));
    }

    /**
     * 前台批量绑定学生
     */
    @ApiOperation("前台批量绑定学生")
    @GetMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestParam("testSessionId") Long testSessionId){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        Binding binding = new Binding();
        binding.setSchoolId(schoolId);
        List<Binding> list = bindingService.selectBindingList(binding);

        List<StudentBinding> studentBindingList = new ArrayList<>();

        if (list.size()>0){
            for (Binding item: list) {
                StudentBinding studentBinding = new StudentBinding();
                studentBinding.setStudentId(item.getUserId());
                studentBinding.setCode(item.getCode());
                studentBinding.setSchoolId(schoolId);
                studentBinding.setTestSessionId(testSessionId);
                studentBinding.setCreateTime(new Date());
                studentBindingList.add(studentBinding);
            }
            studentBindingService.batchDelete(schoolId, testSessionId);
            return AjaxResult.success(studentBindingService.batchAdd(studentBindingList));
        }else{
            return AjaxResult.error("该学校没有已维护的学生绑定数据");
        }
    }

//
//    /**
//     * 修改学生绑定
//     * 接口路径：/studentBinding/front/edit
//     */
//    @ApiOperation("前台修改学生绑定")
//    @PostMapping("/front/edit")
//    public AjaxResult frontEdit(@RequestBody StudentBinding studentBinding)
//    {
//        return toAjax(studentBindingService.updateStudentBinding(studentBinding));
//    }
//
    /**
     * 删除学生绑定
     * 接口路径：/studentBinding/front/delete
     */
    @ApiOperation("前台删除学生绑定")
    @GetMapping("/front/delete")
    public AjaxResult frontRemove(@RequestParam Long[] ids)
    {
        return toAjax(studentBindingService.deleteStudentBindingByIds(ids));
    }
}
